<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
	<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
	<title>菜单管理</title>
	<script type="text/javascript" src="../../js/boot.js"></script>
</head>

<body>
	<div class="mini-splitter" style="width: 100%; height: 100%;">
		<div size="240" showCollapseButton="true">
			<div class="mini-fit">
				<div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
					<a class="mini-button" iconCls="icon-upload" plain="true" onclick="input()">导入</a>
					<a class="mini-button" iconCls="icon-download" plain="true" onclick="output()">导出</a>
				</div>
				<ul id="pageTree" class="mini-tree" style="width: 100%;" showTreeIcon="true" allowDrag="true" allowDrop="true" textField="label"
					idField="menuID" resultAsTree="false">
				</ul>
			</div>
		</div>

		<div showCollapseButton="true">
			<div class="mini-toolbar" style="padding: 2px; border-top: 0; border-left: 0; border-right: 0;">
				<a class="mini-button" iconCls="icon-add" plain="true" onclick="addMenu()">新增</a>
				<a class="mini-button" iconCls="icon-remove" plain="true" onclick="removeMenu()">删除</a>
				<a class="mini-button" iconCls="icon-save" plain="true" onclick="saveMenu()">保存</a>
			</div>

			<div id="dataForm">
				<input id="parentID" name="parentID" class="mini-hidden" />
				<fieldset style="width: 98%;">
					<legend>
						<span>菜单信息</span>
					</legend>
					<div class="fieldset-body">
						<table class="nteport-base-table2" style="width: 100%;">
							<tr>
								<td style="width: 10%; text-align: right;">编号：</td>
								<td style="width: 25%"><input id="menuID" name="menuID" class="mini-textbox" style="width: 90%" enabled="false" /></td>
								<td style="width: 10%; text-align: right;">名称：</td>
								<td style="width: 25%"><input id="label" name="label" class="mini-textbox" style="width: 90%" /></td>
							</tr>
							<tr>
								<td style="text-align: right;">图标：</td>
								<td><input id="icon" name="icon" class="mini-textbox" style="width: 90%" /></td>
								<td style="text-align: right;">路径：</td>
								<td>
									<input class="mini-treeselect" id="path" name="path" textField="label" valueField="path" parentField="parentID"
										checkRecursive="true" multiSelect="false" showFolderCheckBox="false" showClose="true" oncloseclick="onCloseClick"
										popupWidth="300" style="width: 90%" />
								</td>
							</tr>
							<tr>
								<td style="text-align: right;">允许角色：</td>
								<td>
									<div id="authorRole" name="authorRole" class="mini-combobox" style="width:90%;" popupWidth="300" textField="roleName"
										valueField="roleID" multiSelect="true" showClose="true" oncloseclick="onCloseClick">
										<div property="columns">
											<div header="角色名称" field="roleName"></div>
										</div>
									</div>
								</td>
								<td style="text-align: right;">禁用角色：</td>
								<td>
									<div id="forbidRole" name="forbidRole" class="mini-combobox" style="width:90%;" popupWidth="300" textField="roleName"
										valueField="roleID" multiSelect="true" showClose="true" oncloseclick="onCloseClick">
										<div property="columns">
											<div header="角色名称" field="roleName"></div>
										</div>
									</div>
								</td>
							</tr>
							<tr>
								<td style="text-align: right;">排序：</td>
								<td><input id="sequence" name="sequence" class="mini-spinner" minValue="0" style="width:90%" /></td>
								<td style="text-align: right;">状态：</td>
								<td><input id="used" name="used" class="mini-combobox" textField="text" valueField="id" data="CONST.get('useState')"
										style="width:90%" /></td>
							</tr>
						</table>
					</div>
				</fieldset>
			</div>

		</div>
	</div>
</body>

<script type="text/javascript">
	miniInit();

	var tree = mini.get("pageTree");
	http.getSync('/admin/menu/list.do', function (page) {
		tree.loadData(page);
	});
	var editNode = null; // 当前编辑的节点
	/*********************************************************
	 * 左侧目录树的鼠标点击事件
	 *********************************************************/
	tree.on("nodedblclick", function (e) {
		// debugger
		editNode = e.node;
		http.get('/admin/menu/list.do?menuID=' + editNode.menuID, function (page) {
			// 重新展开父节点
			var parent = editNode;
			var children = parent.children;
			tree.removeNodes(children);
			tree.addNodes(page, parent);
			tree.expandNode(parent);
			// 加载表单数据
			if (editNode) {
				pageForm.setData(editNode);
			}
		});
	});

	var pageForm = new mini.Form("#dataForm");

	var pathTree = mini.get("path");
	pathTree.load(http.getUrl("/admin/path/getWebPath.do?path=page&alsoFile=true"));

	var authorRole = mini.get("authorRole");
	authorRole.load(http.getUrl("/role/list.do"));

	var forbidRole = mini.get("forbidRole");
	forbidRole.load(http.getUrl("/role/list.do"));

	/*********************************************************
	 * 操作方法
	 *********************************************************/
	/**
	 * 新增
	 */
	function addMenu() {
		var node = tree.getSelectedNode();
		if (node) {
			var newRow = {
				"parentID": node.menuID
			};
			pageForm.setData(newRow);
			// alert(pageForm.getData().parentID);
		} else {
			miniui.showWarnMessageBox("请先选择菜单创建的位置！");
		}
	}

	/**
	 * 保存
	 */
	function saveMenu() {
		var data = pageForm.getData();
		http.post("/admin/menu/save.do", data, function (page) {
			// debugger
			// 如果是修改
			if (data.menuID) {
				var parent = tree.getParentNode(editNode);
				http.get('/admin/menu/list.do?menuID=' + parent.menuID, function (page) {
					// 重新展开父节点
					var children = parent.children;
					tree.removeNodes(children);
					tree.addNodes(page, parent);
					tree.expandNode(parent);
					// 选中编辑的节点
					var nodes = tree.findNodes(function (node) {
						if (node.menuID == data.menuID) {
							tree.selectNode(node);
							return true;
						}
					}, parent);
				});
			} else {
				var parent = editNode;
				http.get('/admin/menu/list.do?menuID=' + parent.menuID, function (page) {
					// 重新展开父节点
					var children = parent.children;
					tree.removeNodes(children);
					tree.addNodes(page, parent);
					tree.expandNode(parent);
					// 选中父节点
					tree.selectNode(parent);
				});
			}
		});
	}

	/**
	 * 删除
	 */
	function removeMenu() {
		var node = editNode;
		if (node) {
			mini.confirm("确定要删除" + editNode.label + "吗？", "确定？", function (action) {
				if (action == "ok") {
					params = {
						"menuID": node.menuID
					}
					http.post("/admin/menu/delete.do", params, function (page) {
						tree.removeNode(node);
						pageForm.setData([]);
					});
				}
			});
		} else {
			showWarn("请先选择要删除的菜单！");
		}
	}

	/**
	 * 导出
	 */
	function input() { }
	/**
	 * 导出
	 */
	function output() { }
</script>

</html>